package com.wsc.wisdomchina.activitity;
/*
注册页面
 */

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.activitity.fourmodule.ApplyDetailActivity;
import com.wsc.wisdomchina.activitity.fourmodule.CompanyInfoActivity;
import com.wsc.wisdomchina.activitity.fourmodule.FloorEventDetailActivity;
import com.wsc.wisdomchina.activitity.fourmodule.FloorInfoActivity;
import com.wsc.wisdomchina.activitity.fourmodule.HelpOldManDetailActivity;
import com.wsc.wisdomchina.activitity.fourmodule.LifeServerDetailActivity;
import com.wsc.wisdomchina.activitity.fourmodule.PeopleControlActivity;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.fragment.ReceptionCenterFragment;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.StreetInfoListBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.MyToast;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegistActivity extends BaseActivity implements View.OnClickListener, ContributorView {
    private ImageView regist_back;
    private Button ok_button;
    private EditText edit_password, edit_password_two;
    private String phone_num;
    private MyToast myToast;
    private Context context;
    private ContributorPresenter contributorPresenter;
    private final String USER_SET_INFO = "USER_SET_INFO";
    private MyApplication myApplication;
    private TextView tv_headtitle;
    private TextView service_agreement;
    private final String LOCATION_FLAG="LOCATION_FLAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        context = this;
        myToast = new MyToast(context);
        phone_num = getIntent().getExtras().getString("phone_num");
        contributorPresenter = new ContributorPresenter(this, this);
        myApplication=(MyApplication) getApplication();
        initView();
    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("注册");
        regist_back.setOnClickListener(this);
        ok_button = (Button) findViewById(R.id.ok_button);
        ok_button.setOnClickListener(this);
        edit_password = (EditText) findViewById(R.id.edit_password);
        edit_password_two = (EditText) findViewById(R.id.edit_password_two);
        service_agreement=(TextView) findViewById(R.id.service_agreement);
        service_agreement.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.ok_button:
                judge_input(edit_password.getText().toString().trim(), edit_password_two.getText().toString().trim());
                break;
            case R.id.service_agreement:
                Intent intent=new Intent(context,ServiceProcontrolActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {
        if (flag.equals(USER_SET_INFO)) {
            myToast.showShort("正在设置密码,请稍后...");
        }
    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(USER_SET_INFO)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                Message msg = new Message();
                if (respObj.getCode().equals("200")) {
                    if (respObj.getMessage().equals("31")) {
                        msg.what = 200;
                    }
                } else if (respObj.getCode().equals("10502")) {
                    msg.what = 10502;
                } else if (respObj.getCode().equals("10800")) {
                    msg.what = 10800;
                }else{
                    msg.what = -1;
                }
                myHandler.sendMessage(msg);
            }else if(flag.equals(LOCATION_FLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {
                    String message=JsonTools.objectToJsonString(respObj.getMessage());
                    List<StreetInfoListBean> listData=JsonTools.jsonStringToList(message,StreetInfoListBean.class);
                    if(listData!=null &&listData.size()>0)
                    {
                        Tools.setSharedPreferencesStrings(Constant.SharePreferenceLocation,JsonTools.objectToJsonString(listData.get(0)),context);
                        String cidNumber=listData.get(0).getId();
                        //获取注册
                        contributorPresenter.getUserSetInfo(cidNumber,phone_num, edit_password.getText().toString().trim(), USER_SET_INFO);
                    }
                }
            }
        } catch (Exception e) {
            myToast.showShort("操作异常，请稍后再试...");
        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }

    private void judge_input(final String pwd, String pwd2) {
        if (pwd.isEmpty() || pwd2.isEmpty()) {
            myToast.showShort("密码请填写完整");
        } else {
            if (pwd.equals(pwd2)) {

                Pattern p = Pattern
                        .compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$");
                Matcher m = p.matcher(pwd);
                if(!m.matches()){
                    myToast.showShort("密码格式必须6-20位字母与数字组合");
                    return;
                }
                String locationString = Tools.getSharedPreferencesStrings(Constant.SharePreferenceLocation, "", context);
                String cidNumber="";
                if (locationString.length() > 0) {
                    StreetInfoListBean streetInfoListBean = JsonTools.jsonStringToObject(locationString, StreetInfoListBean.class);
                    cidNumber=streetInfoListBean.getId();
                    contributorPresenter.getUserSetInfo(cidNumber,phone_num, pwd, USER_SET_INFO);
                }else
                {
                    //先获取词典Number 再获取注册
                    String city= myApplication.getLocationCity();
                    String country=myApplication.getLocationDistrict();
                    contributorPresenter.getHomeFetchStreet(city,country,LOCATION_FLAG);
                }

            } else {
                myToast.showShort("两次密码不一致");
            }
        }
    }


    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 200:
                    myToast.showShort("密码设置成功,请登录");
                    Intent intent = new Intent(context, LoginActivity.class);
                    startActivityForResult(intent,0);
                    break;
                case 10800:
                    myToast.showShort("密码设置失败");
                    break;
                case 10502:
                    myToast.showShort("该手机号注册过");
                    break;
            }
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == 1) {
                String source=myApplication.getSourceIntoLoginRegisterActivity();
                if(source==null)
                    source=HomeActivity.class.getSimpleName();
                if(source.equals(HomeActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,HomeActivity.class);
                    startActivity(intent);
                }else if(source.equals(FloorInfoActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,FloorInfoActivity.class);
                    startActivity(intent);
                }else if(source.equals(FloorEventDetailActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,FloorEventDetailActivity.class);
                    startActivity(intent);
                }else if(source.equals(CompanyInfoActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,CompanyInfoActivity.class);
                    startActivity(intent);
                }else if(source.equals(HelpOldManDetailActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,HelpOldManDetailActivity.class);
                    startActivity(intent);
                }else if(source.equals(PeopleControlActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,PeopleControlActivity.class);
                    startActivity(intent);
                }else if(source.equals(ApplyDetailActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,ApplyDetailActivity.class);
                    startActivity(intent);
                }else if(source.equals(LifeServerDetailActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,LifeServerDetailActivity.class);
                    startActivity(intent);
                }else if(source.equals(LivingExpensesCountActivity.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,LivingExpensesCountActivity.class);
                    startActivity(intent);
                }
                else if(source.equals(ReceptionCenterFragment.class.getSimpleName()))
                {
                    Intent intent=new Intent(context,ReceptionCenterActivity.class);
                    startActivity(intent);
                }
                else
                {
                    Intent intent=new Intent(context,HomeActivity.class);
                    startActivity(intent);
                }
                finish();
            }
        }
    }

}